//Erstellt von Moritz Liepe
package main.editor;

import main.Module;

import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.draw2d.PolylineConnection;
import org.eclipse.jface.viewers.ISelection;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.handlers.HandlerUtil;


public class DeleteHandler extends AbstractHandler {

	@Override
	public Object execute(ExecutionEvent event) throws ExecutionException {

		
		ISelection selection = HandlerUtil.getActiveWorkbenchWindow(event).getActivePage().getSelection();
		
		if (selection != null & selection instanceof IStructuredSelection) 
		{
			IStructuredSelection strucSelection = (IStructuredSelection) selection;
			if(! strucSelection.isEmpty() && strucSelection.getFirstElement() instanceof Module)
			{
				Module selectionObject = (Module)strucSelection.getFirstElement();
				VOSEditor vosEditor;
				IEditorPart page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
				if((page != null) && (page instanceof VOSEditor)){
					
					vosEditor = (VOSEditor)page;
					vosEditor.getModuleEditor().deleteModule(selectionObject);
				
				}
			}
			
			else if(! strucSelection.isEmpty() && strucSelection.getFirstElement() instanceof PolylineConnection)
			{
				PolylineConnection selectionObject = (PolylineConnection)strucSelection.getFirstElement();
				VOSEditor vosEditor;
				IEditorPart page = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage().getActiveEditor();
				if((page != null) && (page instanceof VOSEditor)){
					
					vosEditor = (VOSEditor)page;
					vosEditor.getModuleEditor().deleteConnection(selectionObject);
				
				}
			}
			
		}
		return null;
	}

}
